#!/usr/bin/env node

let fs = require('fs')

// 检查命令行参数的格式
if (process.argv.length !== 3) {
  console.error('命令行参数格式不正确。正确格式: cmd file_name')
  process.exit(1)
}

// 获取命令行参数中的文件名
const fileName = process.argv[2]

let width = 16
let height = 16

//总字节数目
let pixelByteSize = width * height * 4
//总大小等于总字节数＋头字节数,前54个字节是头信息,后面是数据
let totalSize = pixelByteSize + 54

let buf = Buffer.alloc(totalSize)
buf.fill(0)

//head
  buf.write('BM')
  buf.writeUInt32LE(totalSize, 0x02)
  buf.writeUInt32LE(54, 0x0a)
  buf.writeUInt32LE(40, 0x0e)
  buf.writeUInt16LE(1, 0x1a)
  buf.writeUInt32LE(32, 0x1c)

  buf.writeUInt32LE(pixelByteSize, 0x22)
  buf.writeUInt32LE(width, 0x12)
  buf.writeUInt32LE(height, 0x16)

//data
for (i = 54; i < totalSize; i += 4) {
  buf.writeUInt32LE(0xffff0000, i)
}

fs.writeFile(`./${fileName}`, buf, function(err) {
  if (err) {
    console.error(err)
    process.exit(1)
  }
})